Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
35 / 35 |
| ProductIndexer | |
100.00% |
1 / 1 |
|
100.00% |
6 / 6 |
10 | |
100.00% |
35 / 35 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| index | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
| indexAll | |
100.00% |
1 / 1 |
3 | |
100.00% |
14 / 14 |
|||
| remove | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| removeAll | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
| validateObjectNormalization | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
| <?php | |
| declare(strict_types=1); | |
| namespace Akeneo\Pim\Enrichment\Bundle\Elasticsearch\Indexer; | |
| use Akeneo\Pim\Enrichment\Component\Product\Normalizer\Indexing\ProductAndProductModel\ProductModelNormalizer; | |
| use Akeneo\Tool\Bundle\ElasticsearchBundle\Client; | |
| use Akeneo\Tool\Bundle\ElasticsearchBundle\Refresh; | |
| use Akeneo\Tool\Component\StorageUtils\Indexer\BulkIndexerInterface; | |
| use Akeneo\Tool\Component\StorageUtils\Indexer\IndexerInterface; | |
| use Akeneo\Tool\Component\StorageUtils\Remover\BulkRemoverInterface; | |
| use Akeneo\Tool\Component\StorageUtils\Remover\RemoverInterface; | |
| use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | |
| /** | |
| * Indexer responsible for the indexing of products entities. Each product should be normalized in the right format | |
| * prior to be indexed in the both product and product and product model indexes elasticsearch. | |
| * | |
| * @author Julien Janvier <j.janvier@gmail.com> | |
| * @copyright 2017 Akeneo SAS (http://www.akeneo.com) | |
| * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | |
| */ | |
| class ProductIndexer implements IndexerInterface, BulkIndexerInterface, RemoverInterface, BulkRemoverInterface | |
| { | |
| private const PRODUCT_IDENTIFIER_PREFIX = 'product_'; | |
| /** @var NormalizerInterface */ | |
| private $normalizer; | |
| /** @var Client */ | |
| private $productAndProductModelClient; | |
| /** @var string */ | |
| private $indexType; | |
| /** | |
| * @param NormalizerInterface $normalizer | |
| * @param Client $productAndProductModelClient | |
| * @param string $indexType | |
| */ | |
| public function __construct( | |
| NormalizerInterface $normalizer, | |
| Client $productAndProductModelClient, | |
| string $indexType | |
| ) { | |
| $this->normalizer = $normalizer; | |
| $this->productAndProductModelClient = $productAndProductModelClient; | |
| $this->indexType = $indexType; | |
| } | |
| /** | |
| * Indexes a product in both the product index and the product and product model index. | |
| * | |
| * {@inheritdoc} | |
| */ | |
| public function index($object, array $options = []) : void | |
| { | |
| $normalizedObject = $this->normalizer->normalize( | |
| $object, | |
| ProductModelNormalizer::INDEXING_FORMAT_PRODUCT_AND_MODEL_INDEX | |
| ); | |
| $this->validateObjectNormalization($normalizedObject); | |
| $this->productAndProductModelClient->index($this->indexType, $normalizedObject['id'], $normalizedObject); | |
| } | |
| /** | |
| * Indexes a product in both the product index and the product and product model index. | |
| * | |
| * If the index_refresh is provided, it uses the refresh strategy defined. | |
| * Otherwise the waitFor strategy is by default. | |
| * | |
| * {@inheritdoc} | |
| */ | |
| public function indexAll(array $objects, array $options = []) : void | |
| { | |
| if (empty($objects)) { | |
| return; | |
| } | |
| $indexRefresh = $options['index_refresh'] ?? Refresh::disable(); | |
| $normalizedProductModels = []; | |
| foreach ($objects as $object) { | |
| $normalizedProductModel = $this->normalizer->normalize( | |
| $object, | |
| ProductModelNormalizer::INDEXING_FORMAT_PRODUCT_AND_MODEL_INDEX | |
| ); | |
| $this->validateObjectNormalization($normalizedProductModel); | |
| $normalizedProductModels[] = $normalizedProductModel; | |
| } | |
| $this->productAndProductModelClient->bulkIndexes( | |
| $this->indexType, | |
| $normalizedProductModels, | |
| 'id', | |
| $indexRefresh | |
| ); | |
| } | |
| /** | |
| * Removes the product from both the product index and the product and product model index. | |
| * | |
| * {@inheritdoc} | |
| */ | |
| public function remove($objectId, array $options = []) : void | |
| { | |
| $this->productAndProductModelClient->delete( | |
| $this->indexType, | |
| self::PRODUCT_IDENTIFIER_PREFIX . (string) $objectId | |
| ); | |
| } | |
| /** | |
| * Removes the products from both the product index and the product and product model index. | |
| * | |
| * {@inheritdoc} | |
| */ | |
| public function removeAll(array $objects, array $options = []) : void | |
| { | |
| $objectIds = []; | |
| foreach ($objects as $objectId) { | |
| $objectIds[] = self::PRODUCT_IDENTIFIER_PREFIX . (string) $objectId; | |
| } | |
| $this->productAndProductModelClient->bulkDelete($this->indexType, $objectIds); | |
| } | |
| /** | |
| * Checks the normalized object has the minimum property needed for the indexation to work. | |
| * | |
| * @param array $normalization | |
| */ | |
| protected function validateObjectNormalization(array $normalization) : void | |
| { | |
| if (!isset($normalization['id'])) { | |
| throw new \InvalidArgumentException('Only products with an "id" property can be indexed in the search engine.'); | |
| } | |
| } | |
| } |